package edu.xjdx.os.service.Impl;

import edu.xjdx.os.domain.Discount;
import edu.xjdx.os.domain.DiscountExample;
import edu.xjdx.os.mapper.DiscountMapper;
import edu.xjdx.os.service.DiscountService;
import edu.xjdx.os.utils.IDGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DiscountServiceImpl implements DiscountService {
    @Autowired
    DiscountMapper mapper;

    @Override
    public List<Discount> listAll() {

        return mapper.findAll();
    }

    @Override
    public boolean remove(String id) {
        try {
            mapper.deleteByPrimaryKey(id);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean update(Discount discount) {
        try {
            if(discount.getId()!=null){
                mapper.updateByPrimaryKeySelective(discount);
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public int addDiscount(Discount discount) {
        discount.setId(IDGenerator.getId());
        try {
        mapper.insert(discount);
        return 0;
        }
        catch (Exception e) {
            e.printStackTrace();
            return 1;
        }
    }
}
